<?php

namespace App\Http\Controllers\Api\gao;

use App\Common\Responses;
use App\Models\Live;
use Illuminate\Http\Request;

/**
 * 直播控制器
 */
class LiveController
{

    /**
     * 直播的多条件搜索、展示
     * gao
     * @param Request $request
     * @return false|string
     */
    public function index(Request $request){
        try {
            $params = $request->except('id');
            //多条件搜索
            if (!empty($params)){
                $where = [];
                foreach ($params as $k=>$v){
                    $where[] = ["$k","like","%$v%"];
                }
                $params = Live::findList($where);
                return Responses::success($params);
            }

            //如果传的是id就查详情
            $id = $request->get('id');
            if (!empty($id)){
                $params = Live::findList(['id'=>$id]);
                return Responses::success($params);
            }

            //啥也没传就是查所有
            $params = Live::getAll();
            return Responses::success($params);

        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }

    }

    /**
     * 添加直播
     * gao
     * @param Request $request
     * @return false|string
     */
    public function create(Request $request){
        try {
            $params = $request->validate([
                'title' => 'required',
                'name' => 'required',
                'status' => 'required',
            ]);
            $result = Live::addOne($params);
            if (!$result) throw new \Exception('未知错误，添加失败');
            return Responses::success($params);
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }

    }

    /**
     * 修改直播
     * gao
     * @param Request $request
     * @return false|string
     */
    public function update(Request $request){
        try {
            $id = $request->post('id');
            $params = $request->validate([
                'title' => 'required',
                'name' => 'required',
                'status' => 'required',
            ]);
            $result = Live::saveOne(['id'=>$id],$params);
            if (!$result) throw new \Exception('未知错误，修改失败');
            return Responses::success($params);

        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }


    /**
     * 删除直播
     * gao
     * @param Request $request
     * @return false|string
     */
    public function del(Request $request){
        try {
            $id = $request->get('id');
            $result = Live::delOne($id);
            if (!$result) throw new \Exception('未知错误，删除失败');
            return Responses::success($result);
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }





}
